package com.azt.provider.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.azt.api.pojo.SignApplyReq;
import com.azt.api.pojo.SmsParam;
import com.azt.api.service.MqProducerService;
import com.azt.api.service.MsgConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.azt.api.pojo.CompanySign;
import com.azt.api.pojo.SignApply;
import com.azt.api.service.CompanySignService;
import com.azt.api.service.SignApplyService;
import com.azt.model.page.Pagination;
import com.azt.provider.mapper.CompanySignMapper;
import com.azt.provider.mapper.SignApplyMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
@Transactional
public class SignApplyServiceImpl implements SignApplyService {

	@Autowired
	private SignApplyMapper signApplyMapper;

	@Autowired
	private MsgConfigService msgConfigService;

	@Override
	public Pagination getSignApplyPage(int pageno, int pagesize, Map<String, String> searchMap){
		
		Page<Object> startPage = PageHelper.startPage(pageno, pagesize);
		PageHelper.orderBy("auditState asc, createtime desc");
		List<SignApply> signApplyList = signApplyMapper.selectSignApplyList(searchMap);
		Pagination p = new Pagination(startPage, signApplyList);
		p.setList(signApplyList);
		p.setParams(searchMap);
		return p;
	}
	@Override
	public List<SignApply> querySignListByIds(String ids){
		List<SignApply> list = signApplyMapper.selectByIds(ids);
		return list;
		
	}
	@Override
	public void SaveOrUpdateapply(SignApply apply) {
		if(apply.getId()!=null){
			signApplyMapper.updateByPrimaryKeySelective(apply);
		}else{
			signApplyMapper.insertSelective(apply);
		}
		
	}

	@Override
	public void save(SignApplyReq req) {
		SignApply apply = new SignApply();
		BeanUtils.copyProperties(req, apply);
		apply.setAuditState(0);
		apply.setIdentity(2);
		if (StringUtils.isNotBlank(req.getPurchaseAmount())) {
			apply.setPurchaseAmount(Double.valueOf(req.getPurchaseAmount()));
		}
		signApplyMapper.insert(apply);
		// 发送短信通知运营
		Map<String, String> params = new HashMap<>();
		params.put("companyName", apply.getCompanyName());
		params.put("contacter", apply.getContacter());
		params.put("phone", apply.getPhone());
		msgConfigService.sendMessage("GOLD_APPLY", params);
	}

	@Override
	public void updateAudioOption(Integer id, String auditOpinion) {
		signApplyMapper.updateAudioOption(id, auditOpinion);
	}

}
